﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
using Crawler.Model;
using Husb.Framework.DataAccess;
using Microsoft.Practices.EnterpriseLibrary.Data;

namespace Crawler.DataAccess
{
    public partial class CaptureRuleDao : DataAccessBase<CaptureRuleInfo>
    {
        public CaptureRuleDao()
        {
            //ObjectId = "Id";
            MasterObjectId = "TaskItemId";
            //DetailObjectId = "";
            CommandType = System.Data.CommandType.StoredProcedure;

            GetByIdStatement = "Crawler.CaptureRules_SelectCaptureRule";
            //GetByNameStoredProc = SR.SelectWebPageByName;
            FindAllStatement = "Crawler.CaptureRules_SelectCaptureRulesAll";
            //FindStoredProc = SR.SelectWebPagesDynamic;
            //FindByNameStoredProc = SR.SelectWebPagesByNameInexact;


            InsertStatement = "Crawler.CaptureRules_InsertCaptureRule";
            UpdateStatement = "Crawler.CaptureRules_UpdateCaptureRule";
            //CreateUpdateStoredProc = SR.InsertUpdateWebPage;

            DeleteByIdStatement = "Crawler.CaptureRules_DeleteCaptureRule";
            //DeleteByObjectStoredProc = SR.DeleteWebPage;

            //GetRowCountStoredProc = SR.SelectWebPagesRowCount;
            //GetPagedSortedProc = SR.SelectWebPagesPaged;

            GetByMasterIdStatement = "Crawler.CaptureRules_SelectCaptureRulesByTaskItemId";
        }

        #region PopulateCaptureRuleParamters
        /// <summary>
        /// 根据实体类的属性，给数据库命令的参数赋值
        /// </summary>
        /// <param name="db">数据库对象</param>
        /// <param name="cmd">命令对象</param>
        /// <param name="captureRule"></param>
        protected override void PopulateParamters(CaptureRuleInfo captureRule, Database db, System.Data.Common.DbCommand cmd)
        {
            db.AddInParameter(cmd, "Id", DbType.Guid, captureRule.Id);
            db.AddInParameter(cmd, "Name", DbType.AnsiString, captureRule.Name);
            db.AddInParameter(cmd, "TaskItemId", DbType.Guid, captureRule.TaskItemId);
            db.AddInParameter(cmd, "StartString", DbType.String, captureRule.StartString);
            db.AddInParameter(cmd, "EndString", DbType.String, captureRule.EndString);
            db.AddInParameter(cmd, "ContentType", DbType.Int32, captureRule.ContentType);
            db.AddInParameter(cmd, "Category", DbType.Int32, captureRule.Category);
            db.AddInParameter(cmd, "MatchMethod", DbType.Int32, captureRule.MatchMethod);
            db.AddInParameter(cmd, "MatchRegex", DbType.AnsiString, captureRule.MatchRegex);
            db.AddInParameter(cmd, "ProcessMethod", DbType.Int32, captureRule.ProcessMethod);
            db.AddInParameter(cmd, "Prefix", DbType.String, captureRule.Prefix);
            db.AddInParameter(cmd, "Suffix", DbType.String, captureRule.Suffix);
            db.AddInParameter(cmd, "ReplaceRegex", DbType.AnsiString, captureRule.ReplaceRegex);
        }
        #endregion

        #region  LoadCaptureRuleInfo
        /// <summary>
        /// 根据数据库的查询结果，给实体类的属性赋值
        /// </summary>
        /// <param name="captureRule"></param>
        /// <param name="dr"><see cref="IDataReader" />类的一个实例</param>
        protected override void LoadProperty(CaptureRuleInfo captureRule, System.Data.IDataReader dr)
        {
            captureRule.Id = (System.Guid)dr["Id"];
            captureRule.Name = (System.String)dr["Name"];
            captureRule.TaskItemId = (System.Guid)dr["TaskItemId"];
            captureRule.StartString = dr["StartString"] == DBNull.Value ? "" : (System.String)dr["StartString"];
            captureRule.EndString = dr["EndString"] == DBNull.Value ? "" : (System.String)dr["EndString"];
            captureRule.ContentType = dr["ContentType"] == DBNull.Value ? (int)0 : (System.Int32?)dr["ContentType"];
            captureRule.Category = (System.Int32)dr["Category"];
            captureRule.MatchMethod = dr["MatchMethod"] == DBNull.Value ? (int)0 : (System.Int32?)dr["MatchMethod"];
            captureRule.MatchRegex = dr["MatchRegex"] == DBNull.Value ? "" : (System.String)dr["MatchRegex"];
            captureRule.ProcessMethod = dr["ProcessMethod"] == DBNull.Value ? (int)0 : (System.Int32?)dr["ProcessMethod"];
            captureRule.Prefix = dr["Prefix"] == DBNull.Value ? "" : (System.String)dr["Prefix"];
            captureRule.Suffix = dr["Suffix"] == DBNull.Value ? "" : (System.String)dr["Suffix"];
            captureRule.ReplaceRegex = dr["ReplaceRegex"] == DBNull.Value ? "" : (System.String)dr["ReplaceRegex"];
        }
        #endregion

        #region LoadHalfCaptureRuleInfo
        /// <summary>
        /// 根据执行不同的数据获取命令，得到一个不完整的CaptureRuleInfo<see cref="CaptureRuleInfo" />对象，也许只有部分属性有内容。
        /// </summary>
        /// <param name="captureRule"></param>
        /// <param name="dr">IDataReader<see cref="IDataReader" /></param>
        public static void LoadHalfCaptureRuleInfo(CaptureRuleInfo captureRule, System.Data.IDataReader dr)
        {
            for (int fieldCount = 0; fieldCount < dr.FieldCount; fieldCount++)
            {
                switch (dr.GetName(fieldCount))
                {
                    case "Id":
                        captureRule.Id = (System.Guid)dr["Id"];
                        break;
                    case "Name":
                        captureRule.Name = (System.String)dr["Name"];
                        break;
                    case "StartString":
                        captureRule.StartString = dr["StartString"] == DBNull.Value ? "" : (System.String)dr["StartString"];
                        break;
                    case "EndString":
                        captureRule.EndString = dr["EndString"] == DBNull.Value ? "" : (System.String)dr["EndString"];
                        break;
                    case "ContentType":
                        captureRule.ContentType = dr["ContentType"] == DBNull.Value ? (int)0 : (System.Int32?)dr["ContentType"];
                        break;
                    case "Category":
                        captureRule.Category = (System.Int32)dr["Category"];
                        break;
                    case "MatchMethod":
                        captureRule.MatchMethod = dr["MatchMethod"] == DBNull.Value ? (int)0 : (System.Int32?)dr["MatchMethod"];
                        break;
                    case "MatchRegex":
                        captureRule.MatchRegex = dr["MatchRegex"] == DBNull.Value ? "" : (System.String)dr["MatchRegex"];
                        break;
                    case "ProcessMethod":
                        captureRule.ProcessMethod = dr["ProcessMethod"] == DBNull.Value ? (int)0 : (System.Int32?)dr["ProcessMethod"];
                        break;
                    case "Prefix":
                        captureRule.Prefix = dr["Prefix"] == DBNull.Value ? "" : (System.String)dr["Prefix"];
                        break;
                    case "Suffix":
                        captureRule.Suffix = dr["Suffix"] == DBNull.Value ? "" : (System.String)dr["Suffix"];
                        break;
                    case "ReplaceRegex":
                        captureRule.ReplaceRegex = dr["ReplaceRegex"] == DBNull.Value ? "" : (System.String)dr["ReplaceRegex"];
                        break;
                }
            }
        }
        #endregion
    }
}
